home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / libdes / rand_key.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  4KB  |  108 lines

  1. /* lib/des/rand_key.c */
  2. /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au)
  3.  * All rights reserved.
  4.  * 
  5.  * This file is part of an SSL implementation written
  6.  * by Eric Young (eay@mincom.oz.au).
  7.  * The implementation was written so as to conform with Netscapes SSL
  8.  * specification.  This library and applications are
  9.  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  10.  * as long as the following conditions are aheared to.
  11.  * 
  12.  * Copyright remains Eric Young's, and as such any Copyright notices in
  13.  * the code are not to be removed.  If this code is used in a product,
  14.  * Eric Young should be given attribution as the author of the parts used.
  15.  * This can be in the form of a textual message at program startup or
  16.  * in documentation (online or textual) provided with the package.
  17.  * 
  18.  * Redistribution and use in source and binary forms, with or without
  19.  * modification, are permitted provided that the following conditions
  20.  * are met:
  21.  * 1. Redistributions of source code must retain the copyright
  22.  *    notice, this list of conditions and the following disclaimer.
  23.  * 2. Redistributions in binary form must reproduce the above copyright
  24.  *    notice, this list of conditions and the following disclaimer in the
  25.  *    documentation and/or other materials provided with the distribution.
  26.  * 3. All advertising materials mentioning features or use of this software
  27.  *    must display the following acknowledgement:
  28.  *    This product includes software developed by Eric Young (eay@mincom.oz.au)
  29.  * 
  30.  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  31.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  34.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40.  * SUCH DAMAGE.
  41.  * 
  42.  * The licence and distribution terms for any publically available version or
  43.  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  44.  * copied and put under another distribution licence
  45.  * [including the GNU Public Licence.]
  46.  */
  47.  
  48. #include "des_locl.h"
  49. #include <time.h>
  50.  
  51. static int seed=0;
  52. static des_cblock init;
  53.  
  54. void des_random_seed(key)
  55. des_cblock key;
  56.     {
  57.     memcpy(init,key,sizeof(des_cblock));
  58.     seed=1;
  59.     }
  60.  
  61. void des_random_key(ret)
  62. unsigned char *ret;
  63.     {
  64.     des_key_schedule ks;
  65.     static unsigned long c=0;
  66.     static unsigned short pid=0;
  67.     static des_cblock data={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
  68.     des_cblock key;
  69.     unsigned char *p;
  70.     unsigned long t;
  71.     int i;
  72.  
  73. #ifdef MSDOS
  74.     pid=1;
  75. #else
  76.     if (!pid) pid=getpid();
  77. #endif
  78.     p=key;
  79.     if (seed)
  80.         {
  81.         for (i=0; i<8; i++)
  82.             {
  83.             data[i] ^= init[i];
  84.             init[i]=0;
  85.             }
  86.         seed=0;
  87.         }
  88.     t=(unsigned long)time(NULL);
  89.     l2c(t,p);
  90.     t=(unsigned long)((pid)|((c++)<<16));
  91.     l2c(t,p);
  92.  
  93.     des_set_odd_parity((des_cblock *)data);
  94.     des_set_key((des_cblock *)data,ks);
  95.     des_cbc_cksum((des_cblock *)key,(des_cblock *)key,
  96.         (long)sizeof(key),ks,(des_cblock *)data);
  97.  
  98.     des_set_odd_parity((des_cblock *)key);
  99.     des_set_key((des_cblock *)key,ks);
  100.     des_cbc_cksum((des_cblock *)key,(des_cblock *)data,
  101.         (long)sizeof(key),ks,(des_cblock *)key);
  102.  
  103.     memcpy(ret,data,sizeof(key));
  104.     memset(key,0,sizeof(key));
  105.     memset(ks,0,sizeof(ks));
  106.     t=0;
  107.     }
  108.